package com.javafactroy.kunlong.data.service;


import com.javafactroy.kunlong.base.dto.TQueryParam;
import com.javafactroy.kunlong.base.model.ReturnPageInfo;
import com.javafactroy.kunlong.client.feign.dto.PageInfoDto;
import com.javafactroy.kunlong.data.dao.GeneralSqlMapper;
import com.javafactroy.kunlong.data.factroy.GenericParamFactroy;
import com.javafactroy.kunlong.data.iface.IGeneralService;
import com.javafactroy.kunlong.data.iface.IGenericParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
public class GeneralService<T extends PageInfoDto, M extends GeneralSqlMapper<T>>
        implements IGeneralService<T> {
    @Autowired
    GenericParamFactroy genericParamFactroy;

    //@Autowired
    IGenericParam<T> genricParam;
    // IquerySome<T> iquerySome = (T t) -> {    };

    // IquerySomeList<T> iquerySomeList=(List<T> list)->{};

    @Autowired
    M mapper;

    public int save(T entity) {
        if (entity.checkInsertFlag()) {
            return insert(entity);
        }
        return update(entity);
    }

    public int saveBatch(List<T> entities) {
        for (T e : entities) {
            mapper.insert(e);
        }
        return 0;
    }


    public int insert(T entity) {
        return mapper.insert(entity);
    }

    public int update(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    public int updateNotNull(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    public int deleteByPkeys(T entity) {
        return mapper.deleteByPrimaryKey(entity);
    }

    public int deleteByPkey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int deleteBy(T entity) {
        return mapper.delete(entity);
    }

    public int selectCount(T entity) {
        return mapper.selectCount(entity);
    }

    public T selectByPkey(Long pkey) {
        return mapper.selectByPrimaryKey(pkey);
    }

    public T selectByPkeys(T t) {
        return mapper.selectByPrimaryKey(t);
    }

    public List<T> select(T e) {
        e.startOrderBy();

        return mapper.select(e);
    }

    public List<T> selectAll() {
        return mapper.selectAll();
    }

    public ReturnPageInfo<T> queryParam(@Param("queryParam") TQueryParam<T> queryParam) {
        //if (genricParam != null && queryParam.getParam() == null) {
        //      queryParam.setParam(genricParam.getParam());
        // }

        return query(queryParam.toParam());

    }

    public ReturnPageInfo<T> query(@Param("queryParam") T param) {

        param.startPageOrder();

        List<T> list = mapper.query(param);
        ReturnPageInfo<T> apiPageInfo = new ReturnPageInfo<>();
        apiPageInfo.setPageSize(param.getPageSize());
        apiPageInfo.setPageNum(param.getPageNum());
        return apiPageInfo.valueOf(list);

    }

    public long count(@Param("queryParam") T entity) {
        entity.fields2Col();
        return mapper.count(entity);
    }

}
